package com.jetinno.simple.utils

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

/**
 * Created by zhangyuncai.
 * Date: 2022/3/9
 */
object FragmentsUtil {

    fun sNewStance(context: Context, fragmentPathName: String): Fragment {
        return Fragment.instantiate(context, fragmentPathName, null)
    }

    fun sNewStance(context: Context, clazz: Class<*>): Fragment {
        return Fragment.instantiate(context, clazz.name, null)
    }

    fun sNewStance(context: Context, fragmentPathName: String, bundle: Bundle?): Fragment {
        return Fragment.instantiate(context, fragmentPathName, bundle)
    }

    fun sNewStance(context: Context, clazz: Class<*>, bundle: Bundle?): Fragment {
        return Fragment.instantiate(context, clazz.name, bundle)
    }

    fun <T : DialogFragment> showDialog(
        fragmentManager: FragmentManager,
        context: Context,
        clazz: Class<T>
    ): T {
        return showDialog(fragmentManager, context, clazz, null)
    }

    fun <T : DialogFragment> showDialog(
        fragmentManager: FragmentManager,
        context: Context,
        clazz: Class<T>,
        bundle: Bundle?
    ): T {
        val fragment = sNewStance(context, clazz, bundle) as T
        fragment.show(fragmentManager, clazz.simpleName)
        return fragment
    }
}